<template>
  <div>
    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Overview' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户详情表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="title">
      <h1>用户详情表</h1>
    </el-card>

    <!--卡片视图区域-->
    <el-card>
      <!-- 搜索与添加区域-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"/>-->
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisble = true">添加用户</el-button>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <FlowPreferenceChart/>
        </el-col>
        <el-col :span="12">
          <!-- <SpeedChartDefault></SpeedChartDefault> -->
          <TimeLine/>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <!--用户列表区域-->
        <el-col :span="12">
          <h3 style="text-align: center">流量告警信息</h3>
          <el-table :data="userlist" border stripe size="mini">
            <el-table-column type="index"></el-table-column>
            <el-table-column label="姓名" prop="username"></el-table-column>
            <el-table-column label="邮箱" prop="email"></el-table-column>
            <el-table-column label="电话" prop="mobile"></el-table-column>
            <el-table-column label="角色" prop="role_name"></el-table-column>
            <el-table-column label="操作" width="120px">
              <template slot-scope="scope">
                <!--修改按钮-->
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  size="mini"
                  @click="showEditDialog(scope.row.id)"
                ></el-button>
                <!--删除按钮-->
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  @click="removeUserById(scope.row.id)"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <el-col :span="12">
          <WarningChart></WarningChart>
        </el-col>
      </el-row>

      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import FlowPreferenceChart from '../Overviewchart/FlowPreferenceChart'
// import SpeedChartDefault from '../Overviewchart/SpeedChartDefault'
import WarningChart from '../Overviewchart/WarningChart'
import TimeLine from '../Overviewchart/TimeLineChart'
export default {
  name: 'UserDetailsTable',
  components: {
    FlowPreferenceChart,
    // SpeedChartDefault,
    WarningChart,
    TimeLine
  },
  data() {
    // 验证邮箱的规则
    let checkEmail = (rule, value, callback) => {
      // 验证邮箱的正则
      const regEmail = /^\w+@[a-z0-9]+\.[a-z]+$/i

      if (regEmail.test(value)) {
        // 合法邮箱
        return callback()
      }

      callback(new Error('请输入合法的邮箱'))
    }

    // 验证手机的规则
    let checkMobile = (rule, value, callback) => {
      // 验证手机的正则
      const regMobile = /1[3,5,8]{1}[0-9]{1}[0-9]{8}|0[0-9]{2,3}-[0-9]{7,8}(-[0-9]{1,4})?/gi

      if (regMobile.test(value)) {
        // 合法手机
        return callback()
      }

      callback(new Error('请输入合法的手机'))
    }
    // 验证手机的规则
    return {
      // 获取用户列表的参数对象
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 2
      },
      userlist: [
        {
          username: '王小虎',
          email: 'sdfd21212@qq.com',
          mobile: '21212121',
          role_name: '超级管理员',
          mg_state: true,
          id: 1
        },
        {
          username: '张三',
          email: 'sdfd21212@qq.com',
          mobile: '54545454',
          role_name: '管理员',
          mg_state: false,
          id: 2
        },
        {
          username: '李四',
          email: 'hghg2787872@qq.com',
          mobile: '90908767',
          role_name: '用户',
          mg_state: false,
          id: 3
        }
      ],
      total: 3,
      // 控制添加用户对话框的显示与隐藏
      addDialogVisble: false,
      // 添加用户的表单数据
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      // 添加表单的验证规则对象
      addFormRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      },
      // 控制编辑用户对话框的显示与隐藏
      editDialogVisible: false,
      // 查询到的用户信息对象
      editForm: {},
      // 修改表单的验证规则对象
      editFormRules: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserList()
    this.print()
  },
  methods: {
    async getUserList() {
      //   const { data: res } = this.$http.get('UserRuleTable', { params: this.queryInfo })
      //   console.log(res)
      //   if (res.meta.status !== 200) {
      //     return this.$message.error('获取用户列表失败！')
      //   }
      //   this.userlist = res.data.sideBar
      //   this.total = res.data.total
      console.log('请求用户列表')
    },
    // 监听 pagesize 改变事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    // 监听页码值改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getUserList()
    },

    // 点击按钮 添加用户
    addUser() {
      // 用户的预验证
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 可以发起网络请求
        // const { data: res } = await this.$http.post('UserRuleTable', this.addForm)
        // console.log(res)
        //
        // if (res.meta.status !== 201) {
        //   this.$message.error('添加用户失败！')
        // }
        this.$message.success('添加用户成功')
        // 隐藏添加用户的对话框
        this.addDialogVisble = false
        // 重新获取用户列表
        // this.getUserList()
      })
    },
    // 修改用户信息并提交
    editUserInfo() {
      this.$refs.editFormRef.validate(async valid => {
        console.log(valid)
        if (!valid) return
        // 发起修改用户信息的请求
        // const {data: res} = await this.$http.put('UserRuleTable/' + this.editForm.id, {
        //   email: this.editForm.email,
        //   mobile: this.editForm.mobile
        // })
        //
        // if(res.meta.status !== 200) {
        //   return this.$message.error('更新用户信息失败')
        // }

        // 关闭对话框
        this.editDialogVisible = false
        // 刷新数据列表
        this.getUserList()
        // 提示修改成功
        this.$message.success('更新用户信息成功')
      })
    },
    // 根据Id删除对应的用户信息
    async removeUserById(id) {
      // console.log(id)
      // 询问用户是否删除
      const confirmResult = await this.$confirm(
        '此操作将永久删除该条信息, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(async success => {
          const { data: res } = await this.$http.delete('UserRuleTable/' + id)
          if (res.meta.status !== 200) {
            return this.$message.error('删除用户失败')
          }

          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getUserList()

          return success
        })
        .catch(err => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
          return err
        })
      console.log(confirmResult)
    },
    print() {
      console.log(this.$route.query)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
